﻿using System;

using RayDen.Library.Core.Primitives;
using RayDen.Library.Entity.Frames;

namespace RayDen.Library.Entity.Scene
{

    [Flags]
    public enum LightInfoFlags
    {
        None = 0,
        IsRgb = 0 >> 1,
        ProcessMeshAsTriangleSoup = 1 >> 2,
    }

    [Serializable]
    public class LightsourceInfo
    {
        public LightInfoFlags Flags;
        public LightSourceTypes Type;
        public float[] Spectra;
        public float[] Gain;
        public float Turbulence
        {
            get
            {
                if (this.Type == LightSourceTypes.Sun || Type == LightSourceTypes.Sky)
                    return flParam;
                return 0f;
            }
            set
            {
                if (this.Type == LightSourceTypes.Sun || Type == LightSourceTypes.Sky)
                    flParam = value;
            }
        }

        public Vector Position
        {
            get
            {
                if (this.Type == LightSourceTypes.Point)
                    return vecParam;
                return Vector.Zero;
            }
            set
            {
                if (this.Type == LightSourceTypes.Point)
                    vecParam = value;
            }
        }

        public Vector Direction
        {
            get
            {
                if (this.Type == LightSourceTypes.Sun || Type == LightSourceTypes.Sky)
                    return vecParam;
                return Vector.Zero;
            }
            set
            {
                if (this.Type == LightSourceTypes.Sun || Type == LightSourceTypes.Sky)
                    vecParam = value;
            }
        }

        public string MeshName
        {
            get
            {
                if (this.Type == LightSourceTypes.Area)
                    return strParam;
                return strParam;
            }
            set
            {
                if (this.Type == LightSourceTypes.Area)
                    strParam = value;
            }
        }

        public string ImageName
        {
            get
            {
                if (this.Type == LightSourceTypes.EnvironmentMap)
                    return strParam;
                return strParam;
            }
            set
            {
                if (this.Type == LightSourceTypes.EnvironmentMap)
                    strParam = value;
            }
        }

        public string ProfileName { get; set; }

        private Vector vecParam;
        private string strParam;
        private float flParam;

        public LightsourceInfo(Frames.FrameLightsource lightTag)
        {
            this.Type = lightTag.LightType;
            //this.Direction = lightTag.Parameters[]
            this.Position = lightTag.Parameters.Get<Vector>(FrameLightsource.PointLightPosition);
            this.Spectra = lightTag.Parameters.Get<RgbSpectrum>(FrameLightsource.LightsourcePower).ToArray();
            this.MeshName = lightTag.Parameters.Get<string>(FrameLightsource.AreaLightGeometryName);
            this.ProfileName = lightTag.ProfileName;
            this.ImageName = lightTag.Parameters.Get<string>(FrameLightsource.InfiniteLightMapPath);
            this.Turbulence = lightTag.Parameters.Get(FrameLightsource.SkyLightTurbulence, 1.5f);
            this.Direction = lightTag.Parameters.Get(FrameLightsource.SkyLightSunDir, new Vector(0, 0, 1f));
            this.Gain = new float[3];
        }

    }
}
